#!/usr/bin/python

import sys 
import socket
import string 

server = "92.241.190.37"
channel = "#test"
botnick = "DNSBotAlpha"


def ping():
  ircsock.send("PONG :pingis\n")

def sendmsg(chan , msg):
  ircsock.send("PRIVMSG "+ chan +" :"+ msg +"\n")

def joinchan(chan):
  ircsock.send("JOIN "+ chan +"\n")

ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ircsock.connect((server, 6667))
ircsock.send("USER "+ botnick +" "+ botnick +" "+ botnick +" "+ botnick +"\n")
ircsock.send("NICK "+ botnick +"\n")

while 1:
  ircmsg = ircsock.recv(2048)
  if ircmsg:
    ircmsg = ircmsg.strip('\n\r')
    print(ircmsg)

    if ircmsg.find(":is now your displayed host") != -1:
      joinchan(channel)

    if ircmsg.find("PING :") != -1:
      ping()

    if ircmsg.find('PRIVMSG') !=-1:
      splitmsg(ircmsg) 

    def splitmsg(msg):
      data=msg[1:].split(':',1)
      info=data[0].split(' ')
      message=data[1]
      nick=info[0].split('!')
      if message[0]=='!':
        cmd=message[1:].split(' ')
        msgstr=msg.split(':')
        msgparse1=str(msgstr[2])
        msgparse2=str(msgparse1[6:])
        if cmd[0]=='test':
          ircsock.send("PRIVMSG "+ channel +" :Echo test!\n")
        if cmd[0]=='echo':
          ircsock.send("PRIVMSG "+ channel +" :"+ msgparse2 +"\n")

  else:
    ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    ircsock.connect((server, 6667))
    ircsock.send("USER "+ botnick +" "+ botnick +" "+ botnick +" "+ botnick +"\n")
    ircsock.send("NICK "+ botnick +"\n")
  
